package com.vxb.testmvp.test_zonghe.test_6;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.vxb.testmvp.R;
import com.vxb.testmvp.test_zonghe.test_3.ScoreBase;
import com.vxb.testmvp.util.Util;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.vxb.testmvp.test_zonghe.test_6.FooterState.STATE_NORMAL;


/**
 * Created by User on 2017/4/24.
 */

public class Test6Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<ScoreBase.ScoreData.ScoreContent.Detail> dataList = new ArrayList<>();

    private static final int NORMALLAYOUT = 0;
    private static final int FOOTERLAYOUT = 1;
    private FooterState currentStatus = STATE_NORMAL;
    private FooterHolder footerHolder;

    @Override
    public int getItemCount() {
        return dataList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {//当坐标是最后一个item, 要 返回底部 holder
            return FOOTERLAYOUT;
        } else {
            return NORMALLAYOUT;
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == NORMALLAYOUT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_my_score, parent, false);
            return new NormalHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_common_list_footer, parent, false);
            footerHolder = new FooterHolder(view);
            return footerHolder;
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof NormalHolder) {
            //点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Util.showToast(holder.itemView.getContext(), "点击了按钮");
                }
            });
            ((NormalHolder) holder).setData(position);
        } else {
            ((FooterHolder) holder).setData(currentStatus);
        }
    }


    public void addAll(ArrayList<ScoreBase.ScoreData.ScoreContent.Detail> list) {
        int lastIndex = this.dataList.size();
        if (this.dataList.addAll(list)) {
            notifyItemRangeInserted(lastIndex, list.size());
        }
    }

    class NormalHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_content)
        TextView tv_content;
        @Bind(R.id.tv_time)
        TextView tv_time;
        @Bind(R.id.tv_fixed_score)
        TextView tv_fixed_score;


        public NormalHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(int position) {
            final ScoreBase.ScoreData.ScoreContent.Detail detail = dataList.get(position);
            tv_content.setText(detail.getContent());
            tv_time.setText(detail.getTimes());
            tv_fixed_score.setText(detail.getScore());
        }
    }

    public class FooterHolder extends RecyclerView.ViewHolder {
        View mLoadingViewstubstub;
        View mEndViewstub;
        View mNetworkErrorViewstub;

        public FooterHolder(View itemView) {
            super(itemView);
            mLoadingViewstubstub = itemView.findViewById(R.id.loading_viewstub);
            mEndViewstub = itemView.findViewById(R.id.end_viewstub);
            mNetworkErrorViewstub = itemView.findViewById(R.id.network_error_viewstub);
        }

        //根据传过来的status控制哪个状态可见
        public void setData(FooterState status) {
            Log.d("TAG", "reduAdapter" + status + "");
            setAllGone();
            switch (status) {
                case STATE_NORMAL:
                    break;
                case STATE_LOADING:
                    mLoadingViewstubstub.setVisibility(View.VISIBLE);
                    break;
                case STATE_THE_END:
                    mEndViewstub.setVisibility(View.VISIBLE);
                    break;
                case STATE_NET_WORK_ERROR:
                    mNetworkErrorViewstub.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }

        }

        //全部不可见
        private void setAllGone() {
            if (mLoadingViewstubstub != null) {
                mLoadingViewstubstub.setVisibility(View.GONE);
            }
            if (mEndViewstub != null) {
                mEndViewstub.setVisibility(View.GONE);
            }
            if (mNetworkErrorViewstub != null) {
                mNetworkErrorViewstub.setVisibility(View.GONE);
            }
        }

    }

    public void setFooterHolderState(FooterState footerState) {
        this.currentStatus = footerState;
        if (footerHolder != null) {
            footerHolder.setData(footerState);
        }
    }

    public FooterState getFooterHolderState() {
        return currentStatus;
    }


    public void clearData() {
        dataList.clear();
        notifyDataSetChanged();
        currentStatus = FooterState.STATE_NORMAL;
    }
}
